#include <iostream>
#include <string>
using namespace std;

bool isRight(const string s){
	int pid,tid,pnum=0,tnum=0;
	for( int i=0; i<s.length(); ++i ){
		if( s[i]=='P' ){
			pid = i;
			++pnum;
		} else if( s[i]=='T' ){
			tid = i;
			++tnum; 
		}else if( s[i]=='A') { }
		else{
			return false;
		}
	}
	if( pnum!=1 || tnum!=1 || (tid-pid-1)==0 || (pid*(tid-pid-1)!=s.length()-tid-1) ) return false;
	else return true;
}
int main(){
	freopen("in.txt","r",stdin);
	int n;
	string str;
	cin>>n;
	while( n--){
		cin>>str;
		if( isRight(str) ) cout<<"YES"<<endl;
		else cout<<"NO"<<endl; 
	}
	return 0;
}
